package com.example.yuejian.yuejian.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.activity.UploadPhotoActivity;
import com.example.yuejian.yuejian.adapter.ShowPhotoAdapter;
import com.example.yuejian.yuejian.base.BaseFragment;
import com.example.yuejian.yuejian.base.ModleListListener;
import com.example.yuejian.yuejian.bean.ShowPhotoList;
import com.example.yuejian.yuejian.model.ShowPhotoModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShowPhotoFragment extends BaseFragment {
    private ImageView upload;
      private RecyclerView recyclerView;
    private View view;
    List<ShowPhotoList> list=null;
       //private ShowPhotoAdapter showPhotoAdapter;
    private ModleListListener<ShowPhotoList>modleListListener=new ModleListListener<ShowPhotoList>() {
        @Override
        public void onResponse(List<ShowPhotoList> l) {
         list=l;
            recyclerView.setAdapter(new ShowPhotoAdapter(getActivity(),list));
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getActivity(),"cuowu",Toast.LENGTH_SHORT).show();
        }
    };

    public ShowPhotoFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_show_photo, container, false);
        initViews();
        initEvents();
        return view;
    }
    private void initViews()
    {
        recyclerView=(RecyclerView)view.findViewById(R.id.show_photo_recycleview);
        GridLayoutManager gm = new GridLayoutManager(this.getActivity(),2);
        // 网格是的list布局   2列
        //recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setLayoutManager(gm);
     upload=(ImageView)view.findViewById(R.id.show_photo_upload_btn);
    }
    private void getData()
    {
        ShowPhotoModel model=new ShowPhotoModel();
        model.showPhotoList(modleListListener);
       /* showPhotoAdapter=new ShowPhotoAdapter(getActivity());
        recyclerView.setAdapter(showPhotoAdapter);*/
    }
    private void initEvents()
    {
        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(),UploadPhotoActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onResume() {
        getData();
        super.onResume();
    }
}
